package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanUtils;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.investor.bean.InvestorDealTransInBean;
import com.jrx.anytxn.investor.bean.InvestorDelqTransInSaveBean;
import com.jrx.anytxn.investor.entity.*;
import com.jrx.anytxn.investor.mapper.ext.ExtBtInvestorAccountingTransferListMapper;
import com.jrx.anytxn.investor.service.IInvestorDealDelqService;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: anytxn-cf-parent
 * @description: 联合贷批量-转表内处理
 * @author: xiaoyu.jing
 * @date: 2020-09-01 15:06
 **/
@Order(130)
@Service
public class InvestorTransInHandle extends BaseBatchAccoutHandle<InvestorDelqTransInSaveBean> {

    Logger logger = LoggerFactory.getLogger(InvestorTransInHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private ExtBtInvestorAccountingTransferListMapper transferListMapper;

    @Autowired
    private IInvestorDealDelqService delqService;

    /**
     * 转表内处理业务逻辑
     * @return
     * @throws TxnException
     */
    @Override
    public List<InvestorDelqTransInSaveBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        CmCustomerFirstLevel dealCust = custBatchSaveBean.getDealCust();
        String customerId = dealCust.getCustomerId();
        String tenantId = dealCust.getTenantId();
        PrOrganizationTable org = organizationTableService.findByOrId(TransBizConstant.DEFAULT_ORG_ID,tenantId);
        Date today = org.getToday();
        List<BtInvestorAccountingTransferList> transferListList = transferListMapper.selectByTransList(customerId,tenantId,today);
        if(CollectionUtils.isEmpty(transferListList)){
            return null;
        }

        List<InvestorDelqTransInSaveBean> delqTransInSaveBeanList = new ArrayList<>();
        List<AmLoanMainInvestorInfo> dealLoanMainList = custBatchSaveBean.getDealInvestorLoanMainList();
        List<AmLoanAccountInvestorInfo> dealLoanAcctList = custBatchSaveBean.getDealInvestorLoanAccountList();
        List<AmAccountMainInvestorInfo> dealAcctList = custBatchSaveBean.getDealInvestorAcctList();
        List<AmRepaymentPlanInvestorInfo> dealPlanList = custBatchSaveBean.getDealInvestorPlanList();
        List<AmInterestAccrualInvestorInfo> dealAccuAcctList = custBatchSaveBean.getDealInvestorAccuAcctList();
        for(BtInvestorAccountingTransferList transferList : transferListList){
            logger.info("DelqTransIn处理transferList:{}", BeanUtils.toMap(transferList));
            String investorLoanId = transferList.getInvestorLoanId();;
            AmLoanAccountInvestorInfo loanAccountInfo = dealLoanAcctList.stream()
                    .filter(loanAcct -> investorLoanId.equals(loanAcct.getInvestorLoanId()))
                    .findFirst().orElse(null);
            if (null == loanAccountInfo) {
                logger.info("DelqTransIn处理借据为空");
                continue;
            }

            //添加核销订单的判断 如果是核销订单不做处理
            if(TransBizConstant.YES_FLAG.equals(loanAccountInfo.getChargeoffFlag())){
                logger.info("改订单已经核销不做处理，订单号：{}",loanAccountInfo.getInvestorLoanId());
                continue;
            }
            logger.info("DelqTransIn处理loan:{}", BeanUtils.toMap(loanAccountInfo));

            AmLoanMainInvestorInfo loanMainInfo = dealLoanMainList.stream()
                    .filter(loanMain -> investorLoanId.equals(loanMain.getInvestorLoanId()))
                    .findFirst().get();

            InvestorDealTransInBean dealTransInBean = new InvestorDealTransInBean();
            dealTransInBean.setBusinessDate(today);
            dealTransInBean.setLoanAcct(loanAccountInfo);
            dealTransInBean.setLoanMain(loanMainInfo);
            dealTransInBean.setPlanInfoList(dealPlanList);
            dealTransInBean.setAcctList(dealAcctList);
            dealTransInBean.setAccuAcctList(dealAccuAcctList);
            InvestorDelqTransInSaveBean delqTransInSaveBean = delqService.dealDelqTransIn(dealTransInBean);
            if(null != delqTransInSaveBean){
                delqTransInSaveBeanList.add(delqTransInSaveBean);
            }
        }
        return delqTransInSaveBeanList;
    }
}
